package top.hmtools.callBacks;

import java.util.List;

import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.util.Callback;
import top.hmtools.beans.MapperXmlSqlPartGroupBean;
import top.hmtools.beans.MapperXmlSqlPartTempletBean;
import top.hmtools.enums.EDBType;

public class SqlPartTempletListCallBack   implements     Callback<TreeView<String>, TreeCell<String>> {
    
    private Callback<TreeView<String>, TreeCell<String>> defaultCellFactory;
    private TextField tf_daoMethodName; 
    private TextField tf_factorName;
    private TextField tf_id; 
    private TextField tf_sqlPartName;
    
    private ChoiceBox<String> cb_dataType;
    private ChoiceBox<String> cb_groupId;
    private TextArea ta_comment; 
    private TextArea ta_content;

    public SqlPartTempletListCallBack(
            Callback<TreeView<String>, TreeCell<String>> defaultCellFactory,
            TextField tf_daoMethodName, TextField tf_factorName,
            TextField tf_id, TextField tf_sqlPartName,
            ChoiceBox<String> cb_dataType, ChoiceBox<String> cb_groupId,
            TextArea ta_comment, TextArea ta_content) {
        this.defaultCellFactory = defaultCellFactory;
        this.tf_daoMethodName=tf_daoMethodName;
        this.tf_factorName=tf_factorName;
        this.tf_id=tf_id;
        this.tf_sqlPartName=tf_sqlPartName;
        
        this.cb_dataType=cb_dataType;
        this.cb_groupId=cb_groupId;
        this.ta_comment=ta_comment;
        this.ta_content=ta_content;
    }

    /**
     * 方法说明
     * 输入参数说明
     * 输出参数说明
     */
    @Override
    public TreeCell<String> call(TreeView<String> param) {
        TreeCell<String> result = defaultCellFactory.call(param);
        result.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<Event>(){

            @Override
            public void handle(Event event) {
                TreeCell<String> treeCell = (TreeCell<String>) event.getSource();
                TreeItem<String> treeItem = treeCell.getTreeItem();
                if(treeItem == null){
                	return;
                }
                MapperXmlSqlPartTempletBean userData = (MapperXmlSqlPartTempletBean) treeItem.getGraphic().getUserData();

                tf_daoMethodName.setText(userData.getMethod_name()); 
                tf_factorName.setText(userData.getFactor_name());
                tf_id.setText(userData.getId());
                tf_sqlPartName.setText(userData.getName());
                
                int db_type = userData.getDb_type();
                EDBType edbType=EDBType.getByTypeCode(db_type);
                cb_dataType.setValue(edbType.getName());
                
                List<MapperXmlSqlPartGroupBean> sqlPartGroups = (List<MapperXmlSqlPartGroupBean>) cb_groupId.getUserData();
                for(MapperXmlSqlPartGroupBean tmp:sqlPartGroups){
                    String group_id = userData.getGroup_id();
                    if(group_id.equals(tmp.getId())){
                        cb_groupId.setValue(tmp.getName());
                        break;
                    }
                }
                
                ta_comment.setText(userData.getComment());
                ta_content.setText(userData.getContent());
            }
            
        });
        return result;
    }

}
